<?php
class session
{
    private $soar;
    private $sid;

    public $last_error="";

    public function __construct($soar)
    {/*{{{*/
        $this->soar=$soar;
    }/*}}}*/

    public function __destruct()
    {/*{{{*/
    }/*}}}*/

    public function setsid($sid)
    {/*{{{*/
        $this->sid=$sid;
    }/*}}}*/

    public function getsid()
    {/*{{{*/
        return $this->sid;
    }/*}}}*/

    public function create($uuid)
    {/*{{{*/
        $func="session.create";

        $request="";
        para::put_item($request, $uuid);
        $res=$this->soar->callnode("SESSION", "CREATE",
                $request, $respond);
        if($res == false)
        {
            errlog::write("%s: %s call fail, respond:%s",
                    basename(__FILE__), $func, $respond);
            $this->last_error=SOAR_ERR_COMM_FAIL;
            return null;
        }
        $curr=0;
        $errcode=para::get_item($respond, $curr);
        if(is_null($errcode))
        {
            errlog::write("%s: %s errcode invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_MSG;
            return null;
        }
        if($errcode != SOAR_ERR_OK)
        {
            errlog::write("%s: %s fail, errcode:%s",
                    basename(__FILE__), $func, $errcode);
            $this->last_error=$errcode;
            return null;
        }
        $sid=para::get_item($respond, $curr);
        if(is_null($sid))
        {
            errlog::write("%s: %s sid invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_MSG;
            return null;
        }
        $this->sid=$sid;
        $this->last_error=SOAR_ERR_OK;
        return $sid;
    }/*}}}*/

    public function get($uuid)
    {/*{{{*/
        $func="session.get";

        $request="";
        para::put_item($request, $uuid);
        $res=$this->soar->callnode("SESSION", "GET", $request, $respond);
        if($res == false)
        {
            errlog::write("%s: %s call fail, respond:%s",
                    basename(__FILE__), $func, $respond);
            $this->last_error=SOAR_ERR_COMM_FAIL;
            return null;
        }
        $curr=0;
        $errcode=para::get_item($respond, $curr);
        if(is_null($errcode))
        {
            errlog::write("%s: %s errcode invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_MSG;
            return null;
        }
        if($errcode != SOAR_ERR_OK)
        {
            errlog::write("%s: %s fail, errcode:%s",
                    basename(__FILE__), $func, $errcode);
            $this->last_error=$errcode;
            return null;
        }
        $sid=para::get_item($respond, $curr);
        if(is_null($sid))
        {
            errlog::write("%s: %s sid invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_MSG;
            return null;
        }
        $this->sid=$sid;
        $this->last_error=SOAR_ERR_OK;
        return $sid;
    }/*}}}*/

    public function validate()
    {/*{{{*/
        $func="session.validate";
        if($this->sid == null)
        {
            errlog::write("%s: %s fail, sid invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_PARA;
            return false;
        }

        $request="";
        para::put_item($request, $this->sid);
        $res=$this->soar->callnode("SESSION", "VALIDATE",
                $request, $respond);
        if($res == false)
        {
            errlog::write("%s: %s call fail, respond:%s",
                    basename(__FILE__), $func, $respond);
            $this->last_error=SOAR_ERR_COMM_FAIL;
            return false;
        }
        $curr=0;
        $errcode=para::get_item($respond, $curr);
        if(is_null($errcode))
        {
            errlog::write("%s: %s errcode invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_MSG;
            return false;
        }
        if($errcode != SOAR_ERR_OK)
        {
            errlog::write("%s: %s fail, errcode:%s",
                    basename(__FILE__), $func, $errcode);
            $this->last_error=$errcode;
            return false;
        }

        $this->last_error=SOAR_ERR_OK;
        return true;
    }/*}}}*/

    public function destroy()
    {/*{{{*/
        $func="session.destroy";
        if($this->sid == null)
        {
            errlog::write("%s: %s fail, sid invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_PARA;
            return false;
        }

        $request="";
        para::put_item($request, $this->sid);
        $res=$this->soar->callnode("SESSION", "DESTROY",
                $request, $respond);
        if($res == false)
        {
            errlog::write("%s: %s call fail, respond:%s",
                    basename(__FILE__), $func, $respond);
            $this->last_error=SOAR_ERR_COMM_FAIL;
            return false;
        }
        $curr=0;
        $errcode=para::get_item($respond, $curr);
        if(is_null($errcode))
        {
            errlog::write("%s: %s errcode invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_MSG;
            return false;
        }
        if($errcode != SOAR_ERR_OK)
        {
            errlog::write("%s: %s fail, errcode:%s",
                    basename(__FILE__), $func, $errcode);
            $this->last_error=$errcode;
            return false;
        }
        $this->sid=null;
        $this->last_error=SOAR_ERR_OK;

        return true;
    }/*}}}*/

    public function putkey($pairs)
    {/*{{{*/
        $func="session.putkey";
        if($this->sid == null)
        {
            errlog::write("%s: %s fail, sid invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_PARA;
            return false;
        }

        $request="";
        para::put_item($request, $this->sid, count($pairs));
        foreach($pairs as $key=>$value)
        {
            para::put_item($request, $key, $value);
        }
        $res=$this->soar->callnode("SESSION", "PUTKEY",
                $request, $respond);
        if($res == false)
        {
            errlog::write("%s: %s call fail, respond:%s",
                    basename(__FILE__), $func, $respond);
            $this->last_error=SOAR_ERR_COMM_FAIL;
            return false;
        }
        $curr=0;
        $errcode=para::get_item($respond, $curr);
        if(is_null($errcode))
        {
            errlog::write("%s: %s errcode invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_MSG;
            return false;
        }
        if($errcode != SOAR_ERR_OK)
        {
            errlog::write("%s: %s fail, errcode:%s",
                    basename(__FILE__), $func, $errcode);
            $this->last_error=$errcode;
            return false;
        }
        $this->last_error=SOAR_ERR_OK;

        return true;
    }/*}}}*/

    public function getkey($keys)
    {/*{{{*/
        $func="session.getkey";
        if($this->sid == null)
        {
            errlog::write("%s: %s fail, sid invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_PARA;
            return null;
        }

        $request="";
        para::put_item($request, $this->sid, count($keys));
        foreach($keys as $key)
        {
            para::put_item($request, $key);
        }
        $res=$this->soar->callnode("SESSION", "GETKEY",
                $request, $respond);
        if($res == false)
        {
            errlog::write("%s: %s call fail, respond:%s",
                    basename(__FILE__), $func, $respond);
            $this->last_error=SOAR_ERR_COMM_FAIL;
            return null;
        }
        $curr=0;
        $errcode=para::get_item($respond, $curr);
        if(is_null($errcode))
        {
            errlog::write("%s: %s errcode invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_MSG;
            return null;
        }
        if($errcode != SOAR_ERR_OK)
        {
            errlog::write("%s: %s fail, errcode:%s",
                    basename(__FILE__), $func, $errcode);
            $this->last_error=$errcode;
            return null;
        }
        $num=para::get_item($respond, $curr);
        if(is_null($num))
        {
            errlog::write("%s: %s val_num invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_MSG;
            return null;
        }
        $vals=array();
        for($i=0; $i<$num; ++$i)
        {
            $item=para::get_item($respond, $curr);
            if(is_null($item))
            {
                errlog::write("%s: %s no.%d value invalid",
                        basename(__FILE__), $func, $i+1);
                $this->last_error=SOAR_ERR_MSG;
                return null;
            }
            $vals[]=$item;
        }
        $this->last_error=SOAR_ERR_OK;

        return $vals;
    }/*}}}*/

    public function modkey($key, $val)
    {/*{{{*/
        $func="session.getkey";
        if($this->sid == null)
        {
            errlog::write("%s: %s fail, sid invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_PARA;
            return false;
        }

        $request="";
        para::put_item($request, $this->sid, $key, $val);
        $res=$this->soar->callnode("SESSION", "MODKEY",
                $request, $respond);
        if($res == false)
        {
            errlog::write("%s: %s call fail, respond:%s",
                    basename(__FILE__), $func, $respond);
            $this->last_error=SOAR_ERR_COMM_FAIL;
            return false;
        }
        $curr=0;
        $errcode=para::get_item($respond, $curr);
        if(is_null($errcode))
        {
            errlog::write("%s: %s errcode invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_MSG;
            return false;
        }
        if($errcode != SOAR_ERR_OK)
        {
            errlog::write("%s: %s fail, errcode:%s",
                    basename(__FILE__), $func, $errcode);
            $this->last_error=$errcode;
            return false;
        }
        $this->last_error=SOAR_ERR_OK;

        return true;
    }/*}}}*/

    public function destroykey($key)
    {/*{{{*/
        $func="session.destroykey";
        if($this->sid == null)
        {
            errlog::write("%s: %s fail, sid invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_PARA;
            return false;
        }

        $request="";
        para::put_item($request, $this->sid, $key);
        $res=$this->soar->callnode("SESSION", "DESTROYKEY",
                $request, $respond);
        if($res == false)
        {
            errlog::write("%s: %s call fail, respond:%s",
                    basename(__FILE__), $func, $respond);
            $this->last_error=SOAR_ERR_COMM_FAIL;
            return false;
        }
        $curr=0;
        $errcode=para::get_item($respond, $curr);
        if(is_null($errcode))
        {
            errlog::write("%s: %s errcode invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_MSG;
            return false;
        }
        if($errcode != SOAR_ERR_OK)
        {
            errlog::write("%s: %s fail, errcode:%s",
                    basename(__FILE__), $func, $errcode);
            $this->last_error=$errcode;
            return false;
        }
        $this->last_error=SOAR_ERR_OK;

        return true;
    }/*}}}*/

    public function encrypt($plain)
    {/*{{{*/
        $func="session.encrypt";

        $request="";
        para::put_item($request, $plain);
        $res=$this->soar->callnode("SESSION", "ENCRYPT",
                $request, $respond);
        if($res == false)
        {
            errlog::write("%s: %s call fail, respond:%s",
                    basename(__FILE__), $func, $respond);
            $this->last_error=SOAR_ERR_COMM_FAIL;
            return null;
        }
        $curr=0;
        $errcode=para::get_item($respond, $curr);
        if(is_null($errcode))
        {
            errlog::write("%s: %s errcode invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_MSG;
            return null;
        }
        if($errcode != SOAR_ERR_OK)
        {
            errlog::write("%s: %s fail, errcode:%s",
                    basename(__FILE__), $func, $errcode);
            $this->last_error=$errcode;
            return null;
        }
        $enc=para::get_item($respond, $curr);
        if(is_null($enc))
        {
            errlog::write("%s: %s enc invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_MSG;
            return null;
        }
        $this->last_error=SOAR_ERR_OK;

        return $enc;
    }/*}}}*/

    public function decrypt($enc)
    {/*{{{*/
        $func="session.decrypt";

        $request="";
        para::put_item($request, $enc);
        $res=$this->soar->callnode("SESSION", "DECRYPT",
                $request, $respond);
        if($res == false)
        {
            errlog::write("%s: %s call fail, respond:%s",
                    basename(__FILE__), $func, $respond);
            $this->last_error=SOAR_ERR_COMM_FAIL;
            return null;
        }
        $curr=0;
        $errcode=para::get_item($respond, $curr);
        if(is_null($errcode))
        {
            errlog::write("%s: %s errcode invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_MSG;
            return null;
        }
        if($errcode != SOAR_ERR_OK)
        {
            errlog::write("%s: %s fail, errcode:%s",
                    basename(__FILE__), $func, $errcode);
            $this->last_error=$errcode;
            return null;
        }
        $plain=para::get_item($respond, $curr);
        if(is_null($plain))
        {
            errlog::write("%s: %s enc invalid",
                    basename(__FILE__), $func);
            $this->last_error=SOAR_ERR_MSG;
            return null;
        }
        $this->last_error=SOAR_ERR_OK;

        return $plain;
    }/*}}}*/
}
?>
